package com.store.app.activity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.store.app.BaseActivity;
import com.store.app.ExitApplication;
import com.store.app.R;
import com.store.app.bean.CouponGoCashBean;
import com.store.app.net.NetRequest;
import com.store.app.net.ifc.IOnHttpListener;
import com.store.app.utils.MoneyUtil;
import com.store.app.utils.StringUtils;
import com.store.app.utils.Utils;

import org.json.JSONObject;

import de.greenrobot.event.EventBus;

/**
 * 提现页面--金币
 * Created by Administrator on 2016/10/31.
 */
public class WithDrawalsActivity extends BaseActivity implements IOnHttpListener {

    private ImageView public_ll_return;
    private TextView tvTitle;
    private RelativeLayout rl_parent;
    private NetRequest netRequest;
    private String usable_cash_balance;//可用资产
    private TextView tv_bankname;
    private TextView tv_bankno;
    private String bankname;
    private String bankno;
    private TextView tv_canuse_money;
    private TextView tv_right_join;
    private TextView submit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_withdrawals);
        bankname = getIntent().getStringExtra("bankname");
        bankno = getIntent().getStringExtra("bankno");
        usable_cash_balance = getIntent().getStringExtra("usable_cash_balance");
        if (StringUtils.isEmptyOrNull(usable_cash_balance)) usable_cash_balance = "0.00";

        ExitApplication.getInstance().addActivity(this);
        initView();
        initKeyboardView();
        netRequest = new NetRequest(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onAfinalFail() {
        if (progressBar != null) {
            progressBar.dismiss();
        }
    }

    @Override
    public void onExecuteSuccess(int requestId, String result, String msg) {
        //可用资产
        if (requestId == 1) {
            try {
                JSONObject jsonObject = new JSONObject(result);
                usable_cash_balance = jsonObject.getString("usable_cash_balance");
                tv_canuse_money.setText("¥ " + usable_cash_balance);
                Log.v("zyl", "设置可用金额：" + usable_cash_balance);

                if (!TextUtils.isEmpty(moneytext)) {
                    if (Double.parseDouble(moneytext) >= 0) {
                        //handler.sendEmptyMessage(5);
//                        handler.sendEmptyMessage(CHECKBANKNO);

                        if(Utils.isFastClick()){
                            Log.v("zyl","快速点击了");
                            return;
                        }
                        if (!MoneyUtil.moneyBigThan(usable_cash_balance, "0")) {
                            Toast.makeText(WithDrawalsActivity.this, "可用零钱余额不足",
                                    Toast.LENGTH_LONG).show();
                            return;
                        }

                        if (!TextUtils.isEmpty(moneytext)) {
                            //可用余额与提现金额做比较
                            if (!MoneyUtil.moneyComp(usable_cash_balance, moneytext)) {
                                Toast.makeText(WithDrawalsActivity.this, "可用零钱余额不足",
                                        Toast.LENGTH_LONG).show();
                                return;
                            }
                            //提现金额是否大于0
                            Log.v("zyl", "**:" + !MoneyUtil.moneyComp(moneytext, "100"));
                            if (!MoneyUtil.moneyComp(moneytext, "100")) {
                                Toast.makeText(WithDrawalsActivity.this, "提现金额100元起！",
                                        Toast.LENGTH_LONG).show();
                                return;
                            }
                            showLoading();
                        }
                    }
                }
            } catch (Exception e) {
            }
        } else if (requestId == 4) {
            if (progressBar != null) {
                progressBar.dismiss();
            }
            Toast.makeText(WithDrawalsActivity.this, "提现申请已被受理，预计将在3个工作日转账至您注册时提供的银行账户内。如果有问题，每天惠客服将主动联系您，谢谢合作！",
                    Toast.LENGTH_LONG).show();
            EventBus.getDefault().post(new CouponGoCashBean(2));

            finish();
        }
    }

    @Override
    public void onExecuteFail(int requestId, String msg) {
        if (progressBar != null) {
            progressBar.dismiss();
        }
        Toast.makeText(WithDrawalsActivity.this, msg,
                Toast.LENGTH_LONG).show();
    }

    private void initView() {
        public_ll_return = (ImageView) findViewById(R.id.public_ll_return);
        public_ll_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        tvTitle = (TextView) findViewById(R.id.tvTitle);
        tvTitle.setText("提现");
        rl_parent = (RelativeLayout) findViewById(R.id.rl_parent);
        rl_parent.setBackground(null);
        tv_bankname = (TextView) findViewById(R.id.tv_bankname);
        tv_bankname.setText(bankname);
        tv_bankno = (TextView) findViewById(R.id.tv_bankno);
        tv_bankno.setText(bankno);
        tv_canuse_money = (TextView) findViewById(R.id.tv_canuse_money);
        tv_canuse_money.setText("¥ " + usable_cash_balance);
        tv_right_join = (TextView) findViewById(R.id.tv_right_join);
        tv_right_join.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(WithDrawalsActivity.this,BindBalanceActivity.class);
                startActivity(intent);
            }
        });
    }

    /**
     * 初始化数字键盘弹窗
     */
    private TextView money;
    private String moneytext;
    private ProgressDialog progressBar;
    View.OnClickListener myListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            moneytext = money.getText().toString();
            if (moneytext.contains(".")) {
                String[] tempS = moneytext.split("\\.");
                if (tempS.length > 1) {
                    int resultNum = tempS[1].length();
                    if (resultNum > 1) {
                        return;
                    }
                }
            }
            switch (v.getId()) {
                case R.id.pay_space:
                    moneytext = money.getText().toString();
                    if (!moneytext.contains(".")) {
                        if (moneytext.equals("")) {
                            input_digit("0.");
                        } else {
                            input_digit(".");
                        }
                    }
                    break;

                case R.id.pay_zero:
                    input_digit("0");

                    break;
                case R.id.pay_one:
                    input_digit("1");

                    break;
                case R.id.pay_two:
                    input_digit("2");

                    break;
                case R.id.pay_three:
                    input_digit("3");

                    break;
                case R.id.pay_four:
                    input_digit("4");

                    break;
                case R.id.pay_five:
                    input_digit("5");

                    break;
                case R.id.pay_six:
                    input_digit("6");

                    break;
                case R.id.pay_seven:
                    input_digit("7");

                    break;
                case R.id.pay_eight:
                    input_digit("8");

                    break;
                case R.id.pay_nine:
                    input_digit("9");

                    break;
                default:
                    break;
            }
        }
    };

    private void input_digit(String digit) {
        moneytext = money.getText().toString();
        if (moneytext.indexOf(".") != -1) {
            //获取小数点的位置
            int num = 0;
            num = moneytext.indexOf(".");
            if (moneytext.length() - num > 2) {

            } else {
                moneytext = moneytext + digit;
            }
        } else {
            moneytext = moneytext + digit;
        }
        money.setText(moneytext);
    }

    public void showLoading() {
        progressBar = new ProgressDialog(WithDrawalsActivity.this);
        progressBar.setCancelable(false);
        progressBar.setMessage("提现中...");
        progressBar.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progressBar.setProgress(0);
        progressBar.setMax(100);
        progressBar.show();
        netRequest.requestWithdraw(4, moneytext);
        money.setText("");
    }

    public void initKeyboardView() {
        //请输入金额
        money = (TextView) findViewById(R.id.payee_amount);
        money.setHint("请输入提现金额");
        TextView cancel = (TextView) findViewById(R.id.cancel);
        cancel.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                money.setText("");
            }
        });


        submit = (TextView) findViewById(R.id.submit);
        //弹出框的提现
        submit.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                netRequest.requestUsableCashBalanceFind(1);//查询资产信息
            }
        });
        TextView payee_amount = (TextView) findViewById(R.id.payee_amount);
        TextView pay_one = (TextView) findViewById(R.id.pay_one);
        pay_one.setOnClickListener(myListener);

        TextView pay_two = (TextView) findViewById(R.id.pay_two);
        pay_two.setOnClickListener(myListener);

        TextView pay_three = (TextView) findViewById(R.id.pay_three);
        pay_three.setOnClickListener(myListener);

        TextView pay_four = (TextView) findViewById(R.id.pay_four);
        pay_four.setOnClickListener(myListener);

        TextView pay_five = (TextView) findViewById(R.id.pay_five);
        pay_five.setOnClickListener(myListener);

        TextView pay_six = (TextView) findViewById(R.id.pay_six);
        pay_six.setOnClickListener(myListener);

        TextView pay_seven = (TextView) findViewById(R.id.pay_seven);
        pay_seven.setOnClickListener(myListener);

        TextView pay_eight = (TextView) findViewById(R.id.pay_eight);
        pay_eight.setOnClickListener(myListener);

        TextView pay_nine = (TextView) findViewById(R.id.pay_nine);
        pay_nine.setOnClickListener(myListener);

        TextView pay_zero = (TextView) findViewById(R.id.pay_zero);
        pay_zero.setOnClickListener(myListener);
        TextView pay_space = (TextView) findViewById(R.id.pay_space);
        pay_space.setOnClickListener(myListener);
        TextView pay_del = (TextView) findViewById(R.id.pay_del);
        pay_del.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {

                int index = money.getSelectionStart();
                if (index != 0) {
                    moneytext = money.getText().toString();
                    if (!TextUtils.isEmpty(moneytext)) {
                        moneytext = moneytext.substring(0, moneytext.length() - 1);
                        money.setText(moneytext);
                    }
                }
            }
        });

        payee_amount.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (editable.length() > 0) {
                    submit.setBackgroundResource(R.drawable.border5px_blue_00c3e2);
                    submit.setTextColor(Color.parseColor("#ffffff"));
                } else {
                    submit.setBackgroundResource(R.drawable.border_gray_80ffffff);
                    submit.setTextColor(Color.parseColor("#333333"));
                }
            }
        });
    }
}
